
package net.vinote.smart.platform.service.action;

import java.sql.SQLException;
import java.util.List;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.service.util.AutoInjection;


/**
 * 用户信息表Action类
 * 
 * @author 郑俊伟
 */
public class UserInfoAction
{

	@AutoInjection
	private String userID;


	public String[] getUserRoles(String userId)
	{
		DBManager bm = DBManagerFactory.getInstance("user_role_rela");
		List<BizObject> list;
		String[] roles = null;
		try
		{
			list = bm.createQuery(new String[]
			{
				"roleID"
			}, "userID=?").setParamVal(userId).getResultList();
			roles = new String[list.size()];
			for (int i = 0, n = list.size(); i < n; i++)
			{
				roles[i] = list.get(i).getAttribute("roleID");
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return roles;
	}

	/**
	 * 登录失败数清零
	 * 
	 * @param userID
	 * @return
	 */
	public String clearErrorTimes(String userID)
	{
		DBManager bm = DBManagerFactory.getInstance("user_info");
		int sReturn = 0;
		try
		{
			sReturn = bm.update(new String[]
			{
				"loginerror"
			}, "userid=?", userID);
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return sReturn == 1 ? PlatformConst.SUCCESS : PlatformConst.FAIL;
	}

	/**
	 * 根据userID查询用户名
	 * 
	 * @param userID
	 * @return
	 */
	public String getUserName(String userID)
	{
		DBManager bm = DBManagerFactory.getInstance("user_info");
		BizObject bo;
		try
		{
			bo = bm.createQuery(new String[]
			{
				"userName"
			}, "userid=?").setParamVal(userID).getSingleResult();
			if (bo != null)
			{
				return bo.getAttribute("userName");
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return "";
	}
}
